package com.heima.controller;

import com.heima.common.result.PageResult;
import com.heima.common.result.Result;
import com.heima.pojo.dto.PolicyQueryDTO;
import com.heima.pojo.entity.Policy;
import com.heima.service.PolicyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * \* User: chy
 * \* Date: 2025/8/23  17:19
 * \* Description:策略模块
 * \
 */
@RestController
@RequestMapping("/api/vm-service/policy")
@Slf4j
public class PolicyController {

    @Autowired
    private PolicyService policyService;

    /**
     * 策略条件查询
     * @param policyDTO
     * @return
     */
    @GetMapping("/search")
    public PageResult<Policy> search(PolicyQueryDTO policyDTO) {

        PageResult<Policy> policyVOPageResult = policyService.pageResult(policyDTO);
        return policyVOPageResult;
    }

    /**
     * 新增策略条件
     * @param policy
     * @return
     */
    @PostMapping
    public Result addPolicy(@RequestBody Policy policy) {
        log.info("新增策略条件{}", policy);
        boolean save = policyService.save(policy);
        return Result.success(save);
    }

    /**
     * 删除策略条件
     * @param policyId
     * @return
     */
    @DeleteMapping("/{policyId}")
    public Result deletePolicy(@PathVariable Integer policyId) {
        log.info("根据policyId删除策略条件{}", policyId);
        boolean removeById = policyService.removeById(policyId);
        return Result.success(removeById);
    }

}

